<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/24 0024
 * Time: 12:28
 */

namespace app\api\controller\v1;

use app\api\validate\IDMustBePositiveInt;
use app\api\model\Banner as BannerModel;

// 移除没有用的命名空间：Ctrl+Alt+o
use app\api\validate\TestValidate;
use app\lib\exception\BannerMissException;
use think\Exception;
use think\Validate;

class Banner
{
    /**
     * @param $id
     * 获取指定id的banner信息
     * @url /banner/:id
     * @http GET
     * @id banner的id号
     */
    public function getBanner($id){
//        独立验证
//        验证器
        /*$data = [
            'name' => 'vendor1111111111111111111',
            'email' => 'vendor@qq.com'
        ];*/
        /*$validate = new Validate([
            'name' => 'require|max:10',
            'email' => 'email'
        ]);*/
        /*$validate = new TestValidate();
        $result = $validate->batch()->check($data);
        var_dump($validate->getError());
        var_dump($result);*/

//        AOP 面向切面编程
        (new IDMustBePositiveInt())->goCheck();
//        $banner = BannerModel::with(['items','items.img'])->find($id);
//        get(返回一个记录，模型特有方法), find(返回一个记录，模型/Db特有方法)
//        all(返回一组记录，模型特有方法), select(返回一组记录，Db特有方法)
//        $banner = BannerModel::all($id);//默认查询banner表中id为$id的一条记录
        /*$banner = new BannerModel();
        $banner = $banner->get($id);*/
//        try{
            $banner = BannerModel::getBannerByID($id);
//            $banner->hidden(['delete_time','update_time']);
//            $banner->visible(['id','name','description','items']);
           /* $data = $banner->toArray();
            unset($data['delete_time']);*/
        /*}catch(Exception $ex) {
            $err = [
                'error_code' => 10001,
                'msg' => $ex->getMessage()
            ];
            return json($err, 400);
        }*/
        if (!$banner){
//            log('error');
            throw new BannerMissException();
//            throw new Exception('内部错误');
        }
//        return json($banner);
//        $c = config('setting.img_prefix');
        return $banner;
//        return $data;
    }
}